package com.netdisk.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.netdisk.dao.Dao;
import com.netdisk.entity.FileSystem;

/**
 * 文件系统dao类
 * 
 * @author chenlong 2013-4-13
 */
@Repository(value = "fileSystemDao")
public class FileSystemDao extends Dao<FileSystem> {

	@Override
	public void save(FileSystem obj) {
		Session session = sessionFactory.getCurrentSession();
		session.save(obj);
	}

	@Override
	public void update(FileSystem obj) {
		Session session = sessionFactory.getCurrentSession();
		session.update(obj);
	}

	@Override
	public void delete(FileSystem obj) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(obj);
	}

	@Override
	public List<FileSystem> findAll() {
		Session session = sessionFactory.getCurrentSession();
		List fileSystems = null;
		fileSystems = session.createQuery("from FileSystem").list();
		return fileSystems;
	}

	@Override
	public FileSystem findById(String id) {
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		FileSystem fileSystem = null;
		 
		Query query = session.createQuery("from FileSystem where id=:id");
				fileSystems = query.list();
				query.setString("id", id);
		if (!fileSystems.isEmpty()) {
			fileSystem = fileSystems.get(0);
		}
		return fileSystem;
	}

	@Override
	public List<FileSystem> findByTop(int from, int max) {
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session.createQuery("from FileSystem where status=1");
		query.setFirstResult(from);
		query.setMaxResults(max);
		fileSystems = query.list();
		return fileSystems;
	}

	/**
	 *          
	 * @return true or false
	 */
	public boolean existWithFileName(String ownerId, String parentDir, String fileName) {
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session
				.createQuery("from FileSystem where ownerId=:ownerId and parentDir=:parentDir"
						+ " and  fileName=:fileName add status=1");
		query.setString("ownerId", ownerId);
		query.setString("parentDir", parentDir);
		query.setString("fileName", fileName);
		fileSystems = query.list();
		if (fileSystems.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}
	/**
	 * 
	 * @param ownerId 属主的名字
	 * @param fileName 文件名称
	 * @return 找到的文件
	 */
	public List<FileSystem> findWithOwnerId(String ownerId, String fileName){
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session
				.createQuery("from FileSystem where ownerId=:id"
						+ " and fileName=:fileName and status=1");
		query.setString("id", ownerId);
		query.setString("fileName", fileName);
		fileSystems = query.list();
		return fileSystems;
	}
	public List<FileSystem> findWithCompanyId(String companyId, String fileName){
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session
				.createQuery("from FileSystem where companyId=:companyId"
						+ " and fileName=:fileName and status=1");
		query.setString("companyId", companyId);
		query.setString("fileName", fileName);
		fileSystems = query.list();
		return fileSystems;
	}
	
	public List<FileSystem> findOwnerParentDir(String ownerId, String parentDir){
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session
				.createQuery("from FileSystem where ownerId=:id"
						+ " and parentDir=:parentDir and status=1 order by type desc");
		query.setString("id", ownerId);
		query.setString("parentDir", parentDir);
		fileSystems = query.list();
		return fileSystems;
	}
	
	public List<FileSystem> findCompanyParentDir(String companyId, String parentDir){
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session
				.createQuery("from FileSystem where companyId=:companyId"
						+ " and parentDir=:parentDir and status=1 order by type desc");
		query.setString("companyId", companyId);
		query.setString("parentDir", parentDir);
		fileSystems = query.list();
		return fileSystems;
	}
	
	public List<FileSystem> findFileInTrash(String ownerId){
		Session session = sessionFactory.getCurrentSession();
		List<FileSystem> fileSystems = null;
		Query query = session
				.createQuery("from FileSystem where ownerId=:ownerId"
						+ " and status=0");
		query.setString("ownerId", ownerId);
		fileSystems = query.list();
		return fileSystems;
	}
}
